package com.atguigu.gmall.realtime.publisher.service.impl;

import com.atguigu.gmall.realtime.publisher.MyDateUtil;
import com.atguigu.gmall.realtime.publisher.service.DauService;
import io.searchbox.client.JestClient;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import io.searchbox.core.search.aggregation.TermsAggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DauServiceImpl implements DauService {

    @Autowired
    JestClient jestClient;

    final String INDEX_PREFIX="gmall0609_dau_info_";

    @Override
    public Map getDauMap(String td) {
        Map dauTotalAndHourMapTD = getDauHours(td);
        String yd = MyDateUtil.getYesterday(td);
        Map dauTotalAndHourMapYD = getDauHours(yd);

        //最终组装结果
        Map  dauMap=new HashMap();
        dauMap.put("dauTotal",dauTotalAndHourMapTD.get("total"));
        dauMap.put("dauTd",dauTotalAndHourMapTD.get("hour"));
        dauMap.put("dauYd",dauTotalAndHourMapYD.get("hour"));

        return  dauMap;
    }





  // 查询当日分时计数 和 总数
    public Map getDauHours(String date){
        Map<String,Object> dauTotalAndHourMap =  new HashMap();


        Long total=0L;
        Map dauHourMap = new HashMap();


        String query="{\n" +
                "  \"aggs\": {\n" +
                "    \"groupby_hr\": {\n" +
                "      \"terms\": {\n" +
                "        \"field\": \"hr\",\n" +
                "        \"size\": 24\n" +
                "      }\n" +
                "    }\n" +
                "  }\n" +
                "}";

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms("group_hr").field("hr").size(24);
        searchSourceBuilder.aggregation(aggregationBuilder);

        String indexName=INDEX_PREFIX+date;

        Search search = new Search.Builder(searchSourceBuilder.toString()).addIndex(indexName).addType("_doc").build();
        try {
            SearchResult searchResult = jestClient.execute(search);
          //  total= searchResult.getTotal();
            TermsAggregation groupHr = searchResult.getAggregations().getTermsAggregation("group_hr");
            if(groupHr!=null){
                List<TermsAggregation.Entry> buckets = groupHr.getBuckets();
                for (TermsAggregation.Entry bucket : buckets) {
                    total+=bucket.getCount();
                    dauHourMap.put(  bucket.getKey(),bucket.getCount());
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
            throw  new RuntimeException("查询es 异常！");
        }
        dauTotalAndHourMap.put("total",total);
        dauTotalAndHourMap.put("hour",dauHourMap);



       return dauTotalAndHourMap;

    }
}
